package com.yiquan.video.dao;

import org.hibernate.annotations.Columns;
import org.hibernate.annotations.SQLInsert;
import org.hibernate.annotations.SQLUpdate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import com.yiquan.video.pojo.Video;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 数据访问接口
 *
 * @author Administrator
 */
public interface VideoDao extends JpaRepository<Video, Long>, JpaSpecificationExecutor<Video> {

    @Query(value = "select * from yiquan_video where video_id = ?", nativeQuery = true)
    Video findByVideoId(Long videoId);

    @Query(value = "select comments ,thumbs,forwards,collects from yiquan_video where video_id = ?", nativeQuery = true)
    Map<String, Integer> countVideoOperate(Long videoId);

    @Query(value = "select thumbs from yiquan_video where video_id = ?", nativeQuery = true)
    Integer getThumbsByVideoId(Long videoId);

    @Query(value = "select forwards from yiquan_video where video_id = ?", nativeQuery = true)
    Integer getForwardsByVideoId(Long videoId);

    @Query(value = "select collects from yiquan_video where video_id = ?", nativeQuery = true)
    Integer getCollectsByVideoId(Long videoId);

    @Query(value = "update yiquan_video set thumbs = thumbs+ ?2 where video_id = ?1", nativeQuery = true)
    @Modifying
    @Transactional
    void modifyThumbs(Long videoId, Integer modify);

    @Query(value = "update yiquan_video set collects = collects+ ?2 where video_id = ?1", nativeQuery = true)
    @Modifying
    @Transactional
    void modifyCollects(Long videoId, Integer modify);

    @Query(value = "update yiquan_video set comments = comments+ ?2 where video_id = ?1", nativeQuery = true)
    @Modifying
    @Transactional
    void modifyComments(Long videoId, Integer modify);

    @Query(value = "update yiquan_video set forwards = forwards+ ?2 where video_id = ?1", nativeQuery = true)
    @Modifying
    @Transactional
    void modifyForwards(Long videoId, Integer modify);

    /**
     * 获取视频缩略图
     * @param videoId
     * @return
     */
    @Query(value = "select video_thumbnail from yiquan_video where video_id = ?",nativeQuery = true)
    String findVideoThumbnailByVideoId(Long videoId);


    @Query(value = "select SUM(thumbs) FROM yiquan_video WHERE video_uid = ?1",nativeQuery = true)
    Integer getUserVideoTotalThumbs(Long userId);

    @Query(value = "select * from yiquan_video WHERE  video_uid=?1",nativeQuery = true)
    List<Video> findByUserId(Long userId);

}
